/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package DAO;

import DTO.TinhTrangVeDTO;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;

/**
 *
 * @author Nobita Chau
 */
public class TaoMaHoaDon {
    public static String LayMaHoaDon(String tenstore, String TenMa) throws SQLException{
           String mafinal = null;
           Connection conn = DBConnect.Connect();
            java.sql.CallableStatement st = conn.prepareCall(tenstore);
            st.registerOutParameter(1, Types.VARCHAR);
            st.execute();
            String Ma= st.getString(1);
            int SoMa = 0;
           
            if (Ma == null)
            {
                return TenMa + "00001";
            }
            else
            {
                SoMa = Integer.parseInt(Ma.substring(Ma.length()-5));
                //SoMa = CType(Ma.Substring(2, 5), Integer)
                SoMa = SoMa + 1;
                mafinal = String.valueOf(SoMa);
                String maFINAL = ChuyenSoThanhChuoi(mafinal, TenMa);
                 return maFINAL;
         }
            
           
       
    }
       public static String LayMaHoaDonTenUser(String tenstore, String TenMa, String TenUser) throws SQLException{
           String mafinal = null;
           Connection conn = DBConnect.Connect();
            java.sql.CallableStatement st = conn.prepareCall(tenstore);
            st.registerOutParameter(1, Types.VARCHAR);
            st.execute();
            String Ma= st.getString(1);
            int SoMa = 0;
           
            if (Ma == null)
            {
                return TenMa + "_" + TenUser + "_" + "00001";
            }
            else
            {
                SoMa = Integer.parseInt(Ma.substring(Ma.length()-5));
                //SoMa = CType(Ma.Substring(2, 5), Integer)
                SoMa = SoMa + 1;
                mafinal = String.valueOf(SoMa);
                String maFINAL = ChuyenDoi(mafinal, TenMa, TenUser);
                 return maFINAL;
         }
            
           
       
    }
   
       public static String ChuyenDoi(String Ma, String TenMa , String TenUser)
        {
            if (Ma.length()  == 1)
            {
                return Ma = TenMa + "_" + TenUser + "_" + "0000" + Ma;
            }
            else if (Ma.length() == 2)
            {
               return Ma = TenMa + "_" + TenUser + "_" + "000" + Ma;
            }
            else if (Ma.length() == 3)
            {
               return Ma = TenMa + "_" + TenUser + "_" + "00" + Ma;
            }
            else if (Ma.length() == 4)
            {
                 return Ma = TenMa + "_" + TenUser + "_" + "0" + Ma;
            }
            else if (Ma.length() == 5)
            {
                return Ma = TenMa + "_" + TenUser + "_" + Ma;
            }
            return "";
        }
       
      public static String ChuyenSoThanhChuoi( String Ma, String TenMa)
        {
            if (Ma.length() == 1)
            {
                return Ma = TenMa + "0000" + Ma;
            }
            else if (Ma.length() == 2)
            {
               return Ma = TenMa + "000" + Ma;
            }
            else if (Ma.length() == 3)
            {
               return Ma = TenMa + "00" + Ma;
            }
            else if (Ma.length() == 4)
            {
                return Ma = TenMa + "0" + Ma;
            }
            else if (Ma.length() == 5)
            {
               return Ma = TenMa + Ma;
            }
            
            return"";
        }

    
}
